package com.huabo.monitor.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.JsonBean;
import com.huabo.monitor.entity.TblAssess;
import com.huabo.monitor.entity.TblAssessMarkVo;
import com.huabo.monitor.entity.TblAssessVo;
import com.huabo.monitor.entity.TblOrganization;
import com.huabo.monitor.service.PjgzService;
import com.huabo.monitor.service.TblAssessService;
import com.huabo.monitor.util.ConstClass;
import com.huabo.monitor.util.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@Api(value = "评价管理-评价跟踪", tags = {"评价管理-评价跟踪所有接口"})
@RequestMapping(value = "/nbkz")
public class PjgzController {
    @Resource
    private PjgzService pjgzService;
    @Resource
    private TblAssessService tblAssessService;

    @GetMapping(value = "/pjgl/proj_list")
    @ApiOperation("评价管理-评价跟踪-主页查询")
    public JsonBean proj_listPjgl(
            @ApiParam(name = "pageNumber", value = "pageNumber") @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
            @ApiParam(name = "assNumnber", value = "评价编号") @RequestParam(value = "assNumnber", required = false) String assNumnber,
            @ApiParam(name = "assName", value = "项目名称") @RequestParam(value = "assName", required = false) String assName,
            @ApiParam(name = "startDate", value = "开始时间:格式年-月-日") @RequestParam(value = "startDate", required = false) String startDate,
            @ApiParam(name = "startDates", value = "--开始时间") @RequestParam(value = "startDates", required = false) String startDates,
            @ApiParam(name = "endDate", value = "结束日期") @RequestParam(value = "endDate", required = false) String endDate,
            @ApiParam(name = "endDates", value = "--结束日期") @RequestParam(value = "endDates", required = false) String endDates,
            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) throws Exception {

        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }

        TblStaffUtil staff = DealUserToken.parseUserToken(token);

        boolean isAudit = false;
        TblOrganization organization=this.tblAssessService.queryOrganizationById(staff.getLinkDetp().getOrgid());
        if (organization.getAudittype() != null
                && organization.getAudittype().equals(new BigDecimal(TblOrganization.AUDITTYPE))) {
            isAudit = true;
        }
        IPage<TblAssessVo> iPage=tblAssessService.initiatePjgl( isAudit,staff.getCurrentOrg().getOrgid(),staff.getStaffid(),staff.getRealname(),pageNumber, assNumnber, assName, startDate, startDates, endDate, endDates);
        Map<String, Object> mv = new HashMap<>();
        mv.put("assNumnber", assNumnber);
        mv.put("assName", assName);
        mv.put("startDate", startDate);
        mv.put("startDates", startDates);
        mv.put("endDate", endDate);
        mv.put("endDates", endDates);
        mv.put("pageBean", iPage);
        return new JsonBean(200, "success", mv);
    }


    /**
     * 项目跟踪---评价对象列表
     *
     * @param pageNumber
     * @param selectedPlans
     * @return
     */
    @GetMapping(value = "/pjgl/t08_proj_task")
    @ApiOperation("评价管理-评价跟踪-跟踪")
    public JsonBean t08_proj_task(
            @ApiParam(name = "pageNumber", value = "pageNumber",defaultValue = "1") @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
            @ApiParam(name = "selectedPlans", value = "selectedPlans",defaultValue = "660221") @RequestParam(value = "selectedPlans", required = false) BigDecimal selectedPlans,


            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) throws Exception {

        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }

        TblStaffUtil staff = DealUserToken.parseUserToken(token);


        Map<String, Object> mav = new HashMap<>();

        if (null != selectedPlans) {

            IPage<TblAssessMarkVo> page = new Page(pageNumber, ConstClass.DEFAULT_SIZE);

            IPage<TblAssessMarkVo> pageBean = this.pjgzService.findMarkByOrgGroup(page, selectedPlans);
            TblAssess assess = tblAssessService.getById(selectedPlans);

            mav.put("pageBean", pageBean);
            mav.put("project",assess);
        }
        return new JsonBean(200,"success", mav);
    }


    @ApiOperation("评价跟踪-跟踪-点击评价对象(评价人列表)")
    @GetMapping(value = "/pjgl/t08_proj_person")
    public JsonBean t08_proj_person(
            @ApiParam(name = "pageNumber", value = "pageNumber",defaultValue = "1") @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
            @ApiParam(name = "selectedPlans", value = "selectedPlans",defaultValue = "660221") @RequestParam(value = "selectedPlans", required = false) BigDecimal selectedPlans,
            @ApiParam(name = "orgid", value = "orgid",defaultValue = "127636") @RequestParam(value = "orgid", required = false) BigDecimal orgid,
            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) throws Exception {

        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
            Map<String, Object> mav = new HashMap<>();
            IPage<Map<String,Object>> page = new Page(pageNumber, ConstClass.DEFAULT_SIZE);

            pjgzService.findByperson(page,selectedPlans,orgid);

            mav.put("pageBean", page);
            mav.put("selectedPlans", selectedPlans);
            mav.put("orgid", orgid);

            return new JsonBean(200,"success", mav);
    }

}



